package com.gec.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gec.entity.Cart;
import com.gec.entity.CartItem;
import com.gec.service.ProductService;

@WebServlet("/AddCartServlet")
public class AddCartServlet extends HttpServlet{
	ProductService productServlet = new ProductService();
	
	protected void doGet(HttpServletRequest request,HttpServletResponse response)
		throws ServletException,IOException{
		String parameter = request.getParameter("pid");
		int pid = Integer.parseInt(parameter);
		int buyNum =Integer.parseInt(request.getParameter("buyNum"));
		ProductService productService = new ProductService();
		CartItem cartItem =productService.createCartItem(pid,buyNum);
		HttpSession session = request.getSession();
		Cart cart =(Cart)session.getAttribute("cart");
		if(cart==null) {
			cart=new Cart();
		}
		Map<String,CartItem> cartItems = cart.getCartItems();
		cartItems.put(parameter, cartItem);
		session.setAttribute("cart", cart);
		response.sendRedirect("cart.jsp");
	}
	
}